<?php
class AdminNoticeForm extends Form{

	function AdminNoticeForm(){		
		Form::Form('AdminNoticeForm');
		CGlobal::$website_title="Thông báo của BQT";		
		$this->link_css('style/manage_item.css');
		$this->link_css('style/aznet/ui.datepicker.css');		
		$this->link_js('javascript/jquery/ui.datepicker.js');							
	}
	
	function on_submit(){
		$ids = (isset($_POST['chk_id']))?$_POST['chk_id']:array(); 							
		if(count($ids)>0){		
			$all_id = '';				
			for($i=0;$i<count($ids);$i++){
				$all_id .= $ids[$i].',';			
			}	
			$all_id = substr($all_id,0,-1);
			AdminNotice::delNotice($all_id);
			User::getAdminNotice(0,1);			
			Url::redirect_url(Url::build_all(array('chk_id','del_all','cmd','id')));
		}
	}
	
	function draw(){	
		$this->beginForm();	
		global $display;
		
		$display->add('created_time_from',Url::get('created_time_from'));
		$display->add('created_time_to',Url::get('created_time_to'));	
		$display->add('sender_user_name',Url::get('sender_user_name'));
		
		$created_time_from=0;		
		$created_time_to=0;
		$item_per_page = 50;
		$limit = '';
		
		if(Url::get('created_time_from')){
			$date_arr = explode('-',Url::get('created_time_from'));
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		if(Url::get('created_time_to')){
			$date_arr = explode('-',Url::get('created_time_to'));
			
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		$search_value='';
		if($created_time_from){																	
			$search_value .= ' time_add >= '.$created_time_from;
		}
		
		if($created_time_to){
			if($created_time_from){
				$and = ' AND ';
			}
			else{
				$and = ' ';
			}																	
			$search_value .= $and;
									
			$search_value .= ' time_add <= '.$created_time_to;
		}
		
		// search ô textbox						
		if($search_value){	
			$sql_count = 'SELECT COUNT(*) AS total_item FROM admin_notice WHERE  '.$search_value;		
		}
		else{
			$sql_count='SELECT COUNT(*) AS total_item FROM admin_notice';		
		}
		
		$total=DB::fetch($sql_count,'total_item',0);		
		$items=array();
		
		if($total)
		{
			$paging = AZPagging::pagingSE($limit,$total,$item_per_page,10,'page_no',true,'Thông báo','Trang');
			
			if($search_value){			
				$sql = 'SELECT * FROM admin_notice WHERE '.$search_value.' ORDER BY id DESC '.$limit;
			}
			else{
				$sql = 'SELECT * FROM admin_notice ORDER BY id DESC '.$limit;
			}
			
			$result = DB::query($sql);
			
			if($result){
				while ($row=mysql_fetch_assoc($result)){
					if($row["admin_add"] && $row["time_add"]){
						$row["add"] = "<a target='_blank' href='".$row["admin_add"]."'>".$row["admin_add"]."</a> thêm lúc ".date("d-m-y H:i", $row["time_add"]);;
					}
					
					if($row["admin_edit"] && $row["time_edit"]){
						$row["edit"] = "<a target='_blank' href='".$row["admin_edit"]."'>".$row["admin_edit"]."</a> sửa lúc ".date("d-m-y H:i", $row["time_edit"]);;
					}
					
					$items[$row['id']]=$row;
				}				
			}
		}
		else{
			$paging = '';
		}			
		
		$display->add('items',$items);
		$display->add('paging',$paging);		
		$display->output('AdminNotice');
		
		$this->endForm();		
	}
}
?>